package org.easybuy.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;



public class DBUtil {
	// 数据库连接池对象
	private static BasicDataSource ds;
	// 类加载的时候，初始化连接池
	static {
		Properties p = new Properties();
		try {
			p.load(DBUtil.class.getClassLoader().getResourceAsStream(
					"database.properties"));
			String driver = p.getProperty("driver");
			String url = p.getProperty("url");
			String user = p.getProperty("user");
			String password = p.getProperty("password");
			// 连接池的最大连接
			int maxactive = Integer.parseInt(p.getProperty("maxactive"));
			// 没有连接的最大等待时间
			int maxwait = Integer.parseInt(p.getProperty("maxwait"));
			ds = new BasicDataSource();
			ds.setDriverClassName(driver);
			ds.setUrl(url);
			ds.setUsername(user);
			ds.setPassword(password);
			ds.setMaxActive(maxactive);
			ds.setMaxWait(maxwait);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 获得连接
	public static Connection getConnection() throws SQLException {
		Connection connection = ds.getConnection();
		return connection;
	}

	public static void main(String[] args) throws SQLException {
		Connection conn = DBUtil.getConnection();
		if (conn != null) {
			System.out.println("a");
		}
	}
}
